package com.lq.demo.demo.mode.limit;

import com.lq.demo.demo.mode.aop.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 这个简单的controller用来演示aop的
 */
@RestController
@RequestMapping("/demo")
public class TestController {

private TimerService timerService;

    @GetMapping("/test")
    public String demo1(HttpServletRequest request) {
        synchronized (this) {
            System.out.println("  do  somethng ");

            ServletContext servletContext = request.getServletContext();
            int limit = (int) servletContext.getAttribute("limit");
            if (limit <= 0) {
                throw new RuntimeException("次数限制");
            }
            servletContext.setAttribute("limit", limit-1);
            int newlimit = (int) servletContext.getAttribute("limit");
            System.out.println("  newlimit:" + newlimit);
        }
        return "test limit success ";
    }

}
